// set cartCookie
import globalData from '@/main.js';
import { showToast } from 'vant';
import Cookies from 'vue-cookies'
const axios = require('axios');

export function setCookie(shopInfo, buyCount, cook) {
	//console.log("setCookie is", shopInfo)
	//console.log("setCookie cook is", cook)
	//console.log("setCookie shopid is", shopInfo.shopid)
	//console.log("setCookie shopid 22 is", shopInfo['shopid'])
	var shopid = shopInfo.shopid;
	if (buyCount <= 1) {
		buyCount = 1;
	} else if (buyCount >= 100) {
		buyCount = 100;
	}
	// var Cartlist = this.$cookies.get('Cartlist');
	var cartList = Cookies.get('Cartlist');
	//console.log("setCookie cartList is", cartList)
	var info;
	if (!cartList) {
		info = {};
	} else {
		info = cartList;
		//console.log("info is", info)
		//console.log("info type is", typeof info)
		if ((typeof info) !== 'object') {
			info = {};
		}
	}
	// if(!shopInfo.id){
	// 	var CartTotal=$("#sCartTotal").text();
	// 		$("#sCartTotal").text(parseInt(CartTotal)+1);
	// 		$("#btnMyCart em").text(parseInt(CartTotal)+1);
	// }	
	info[shopid] = {};
	info[shopid]['num'] = buyCount;
	info[shopid]['shenyu'] = shopInfo['shenyu'] - buyCount;
	info[shopid]['money'] = shopInfo['money'] * buyCount;
	info['MoenyCount'] = info[shopid]['money'];
	//console.log("setCookie new info is", info)
	// Cookies.set('Cartlist',JSON.stringify(info),{expires:'1d'})
	Cookies.set('Cartlist', info, {
		expires: '1d'
	})
	// Cookies('Cartlist',$.toJSON(info),{expires:7,path:'/'});
	// if(cook){
	// 	window.location.href="{WEB_PATH}/member/cart/cartlist/"+new Date().getTime();//+new Date().getTime()
	// }
}

// 将unixtime转换成日期格式
export function convertUnixToDate(unixTimestamp) {
	// console.log("unixTimestamp:",unixTimestamp)
	var dateObj = new Date(unixTimestamp * 1000);
	// console.log("dateObj:",dateObj)
	// return dateObj.toISOString();
	//return dateObj.toLocaleString();
	return dateObj.toUTCString();
}

export function botimInit() {
	//console.log(window.BOT);
	window.BOT.Run(($rootScope, BOTBridge) => {
		// Framework loaded, but JSSDK has not initialized & launched yet
		// DO NOT PUT ANY Calls here
		//BOTBridge.onLaunch(function() {
		//console.log(1212)
		BOTBridge.onLaunch(() => {
	
			//console.log("BOTIM launched successfully!");
	
			window.Bridge = BOTBridge;
			// you can start using window.Bridge
			window.BOT = BOTBridge;
			//console.log("===bitimInit window info===")
			//console.log("bitimInit window:", window)
			//console.log("bitimInit window.Bridge:", window.Bridge)
			//console.log("bitimInit ===window info end===")
			 authClickHandler()
		})
	
	})
}

export function login() {
	
	window.BOT.Run(($rootScope, BOTBridge) => {
		// Framework loaded, but JSSDK has not initialized & launched yet
		// DO NOT PUT ANY Calls here
		//BOTBridge.onLaunch(function() {
		BOTBridge.onLaunch(() => {
	
			//console.log("BOTIM launched successfully!");
	
			window.Bridge = BOTBridge;
			// you can start using window.Bridge
			window.BOT = BOTBridge;
			//console.log("===login window info===")
			//console.log("login window:", window)
			//console.log("login window.Bridge:", window.Bridge)
			//console.log("login ===window info end===")
			// window.Bridge.getAuthcode({
			// 	redirectUri: 'https://botim.me/mp/b/?appp=com.173games.treasureh5',
			// 	state: '',
			// 	scope: ''
			// }, {
			// 	success: (res) => {
			// 		console.log("res:",res); // res.data.code
			// 		showToast(res.data.code)
			// 	}
			// })
			authClickHandler()
		})
	
	})
	// const isBotim = () => {
	//  var u = navigator.userAgent
	//  return u.match(/BOTIM/)
	// }
	// console.log("isBotim:"+isBotim)
	
	console.log("...login...")
}

export function getUserInfo() {
	//console.log("===getUserInfo===")
	return new Promise((resolve, reject) => {
		window.Bridge.getUserInfo({
			success: res => {
				console.log(res.data);
				resolve(res.data)
			},
			fail: res => {
				Toast('Failed to get user information.');
				reject();
			}
		})
	})
}
export async function auth() {
	//console.log("===auth===")
	return new Promise((resolve, reject) => {
		window.Bridge.getAuthcode({
			state: '',
			scope: '',
			redirectUri: 'https://botim.me/mp/b/?appp=com.173games.treasureh5'
		}, {
			success: function(res) {
				console.log("=== auth res===",res);
				resolve(res.data);
			},
			fail: function(err) {
				reject();
			}
		})
	})
}

export async function authClickHandler() {
	//console.log("===authClickHandler===")
	await auth().then(function(data) {
		console.log("===authClickHandler auth data===",data);
	});
	const userInfo = await getUserInfo();
	//console.log("===authClickHandler userInfo===", userInfo)
	
	if(userInfo == null) {
		return userInfo;
	}
	
	//const contaner = document.querySelector('#user_money_info3');
	$cookies.set('user',userInfo,{
		expires: '10m'
	})
	
	window.Bridge.setStorageSync('user',userInfo)
	
	return userInfo;
}

export function botimShare(_title,_content) {
	
	// window.BOT.Bridge.share()
	// console.log("===window===",window)
	console.log("===window.BOT===",window.BOT)
	// console.log("===window.Bridge===",window.Bridge)
	// console.log("===window.Bridge.share===",window.Bridge.share)
	//showToast(window)
	window.BOT.share()
	// window.BOT.shareContent({
	// 	title: _title,
	// 	content: _content
	// })
}

export function botimPay(tradeIdStr) {
	
	// window.BOT.Bridge.share()
	// console.log("===window===",window)
	console.log("===window.BOT===",window.BOT)
	// console.log("===window.Bridge===",window.Bridge)
	// console.log("===window.Bridge.share===",window.Bridge.share)
	//showToast(window)
	//window.Bridge.share()
	window.BOT.requestPayment(
	    {
	        "tradeId":tradeIdStr,
	    },
	    {
	        success: (res) => {
	            console.log(res)
	        },
	        fail: (err) => {
	            console.error(err.data.code)
	        }
	    }
	)
	
}
